package org.example;

public class Test {

    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length, sum = 0, len = Integer.MAX_VALUE;
        for (int left = 0, right = 0; right < n; right++) {
            sum += nums[right]; //进窗口
            while (sum >= target) {
                len = Math.min(len, right - left + 1);//更新结果
                sum -= nums[left++];//出窗口
            }
        }
        return len == Integer.MAX_VALUE ? 0 : len;
    }

    public int lengthOfLongestSubstring(String s) {
        char[] ss = s.toCharArray();
        int left = 0, right = 0, n = s.length();
        int ret = 0;
        int[] hash = new int[128];
        while (right < n) {
            hash[ss[right]]++; //进窗口
            while (hash[ss[right]] > 1) { //判断
                hash[ss[left++]]--; //出窗口
            }
            ret = Math.max(ret, right - left + 1); //更新结果
            right++;
        }
        return ret;
    }
}
